package com.abigdreamer.im.client.module.login;

import com.abigdreamer.im.client.module.network.socket.ClientChatSocket;
import com.abigdreamer.im.client.ui.BaseFrame;
import com.abigdreamer.im.client.ui.trayIcon.EIMTrayIcon;
import com.abigdreamer.im.client.util.ClientIMUtil;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.im.message.LoginMessage;
import com.abigdreamer.swing.app.LoginWindow;
import com.abigdreamer.swing.component.JCButton;

import craky.componentc.JCComboBox;
import craky.componentc.JCMessageBox;
import craky.layout.LineLayout;

public class EIMLoginWindow extends LoginWindow {

	private static final long serialVersionUID = -7434638012627650136L;

	private static EIMLoginWindow instance;

	private JCComboBox state_ComboBox; // 状态ComboBox

	private JCButton set_Button;

	private LoginMessage loginMessage;

	public static EIMLoginWindow getInstance() {
		if (instance == null) {
			instance = new EIMLoginWindow();
		}
		return instance;
	}

	public EIMLoginWindow() {
		super("", "logo_16.png", 334, 218);

		
		state_ComboBox = ClientIMUtil.createIMStateCombox();
		
		ecStatus.add(state_ComboBox, LineLayout.START);

		set_Button = new JCButton("设置↓");
		set_Button.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				set_ButtonMouseClicked(evt);
			}
		});
		bottomPane.add(set_Button, LineLayout.START);

		Thread trayIconThread = new Thread(EIMTrayIcon.getInStance());
		trayIconThread.start();
	}

	public void changTryIcon() {
		EIMTrayIcon qqTrayIcon = EIMTrayIcon.getInStance();
		qqTrayIcon.setFrame(this);
		qqTrayIcon.setTrayIconState(EIMClientConfig.OffLineTryIcon_Type);
		qqTrayIcon.showIcon(EIMClientConfig.OffLineTryIcon_Type);
	}

	/**
	 * 登录界面设置按钮的事件
	 */
	private void set_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
		EIMNetSetFrame netSetFrame = EIMNetSetFrame.GetInstance();
		BaseFrame.centerWindow(netSetFrame);
		netSetFrame.setVisible(true);
		this.setEnabled(false);
		this.setVisible(false);
	}

	@Override
	protected void onLogin(String userName, char[] password) {
		if (initQQAcount(userName, password)) {
			java.awt.EventQueue.invokeLater(new Runnable() {
				public void run() {
					WaitLoginFrame waitingFrame = WaitLoginFrame.getInstance();
					BaseFrame.leftWindow(waitingFrame);
					waitingFrame.setVisible(true);
					waitingFrame.changTryIcon();
				}
			});
			this.startLoginServer();
			this.setVisible(false);
		}
	}

	private boolean initQQAcount(String userName, char[] password) {
		if (null == userName || userName.equals("")) {
			JCMessageBox.createWarningMessageBox(this, "提示", "请输入ID!!..").open();
			return false;
		}
		int state_num = state_ComboBox.getSelectedIndex();
		String state = state_ComboBox.getItemAt(state_num).toString();

		String pwd = String.valueOf(password);

		if (pwd.equals("") || pwd == null) {
			JCMessageBox.createWarningMessageBox(this, "提示", "请输入密码!!..").open();
			return false;
		}
		loginMessage = new LoginMessage();
		loginMessage.setSrcQQ(userName);
		loginMessage.setPwd(pwd);
		loginMessage.setOwerQQ(userName);
		loginMessage.setState(state);
		return true;
	}

	/**
	 * 发送登入消息方法
	 */
	public void startLoginServer() {
		ClientChatSocket chatSocket = ClientChatSocket.getInstance();
		if (!chatSocket.startSocket()) {
			JCMessageBox.createWarningMessageBox(this, "提示", "网络连接错误！！").open();
			return;
		}
	}

	public LoginMessage getLoginMessage() {
		return loginMessage;
	}

	@Override
	protected boolean checkUserExist(String name) {
		// return !UserDAO.getUsers(Restrictions.eq("name", name)).isEmpty();
		return false;
	}

	@Override
	protected void openUserInfoDialog(String bannerName, LoginWindow loginWindow, String userName) {
		// UserInfoDialog dialog = new UserInfoDialog(bannerName, loginWindow,
		// userName, true);
		// dialog.showWindow();
	}
}
